package com.meirengu.webview.service.impl;

import com.meirengu.channel.service.WeidaiService;
import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.KwInterfaceConfig;
import com.meirengu.webview.model.KwWeidaiFilter;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwWeidaiFilterService;
import com.meirengu.webview.service.UserInfoService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * KwWeidaiFilter服务实现层
 *
 * @author Mao
 * @create Wed May 09 11:17:13 CST 2018
 */
@Service
public class KwWeidaiFilterServiceImpl extends BaseServiceImpl<KwWeidaiFilter> implements KwWeidaiFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private WeidaiService weidaiService;


    Logger logger = LoggerFactory.getLogger(KwWeidaiFilterServiceImpl.class);


    /**
     * 发送微贷网接口
     * 过滤条件
     * 1.区域除了西藏和港澳台都可以
     * 2.有车
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendWeidai(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String weidaiIol = redisClient.get("iol-weidai");
            if ("N".equals(weidaiIol)) {
                Date brithdayDate = userInfo.getBirthday();
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                Integer hasCar = userInfo.getCar();
                BigDecimal expectAmount = userInfo.getExpectAmount();

                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwWeidaiFilter kwWeidaiFilter = new KwWeidaiFilter();
                kwWeidaiFilter.setUserId(id);
                kwWeidaiFilter.setMobile(mobile);
                kwWeidaiFilter.setChannelCode("weidai");
                kwWeidaiFilter.setChannelName("微贷网");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("weidai");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwWeidaiFilter.setSuited("N");
                            kwWeidaiFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwWeidaiFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwWeidaiFilter.setSuited("N");
                            kwWeidaiFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwWeidaiFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwWeidaiFilter.setSuited("N");
                            kwWeidaiFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwWeidaiFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(hasCar.intValue() + "")) {
                            dataFlag = false;
                            kwWeidaiFilter.setSuited("N");
                            kwWeidaiFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwWeidaiFilter.setRefuseMsg("没有车：" + hasCar.intValue());
                        }
                    }
                    if (dataFlag) {
                        Integer code = weidaiService.applyLoan(name, mobile, (sex + 1) + "", city);
                        kwWeidaiFilter.setSuited("Y");
                        kwWeidaiFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "weidai", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwWeidaiFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

}
